<!-- 表格组件 -->
<script lang="tsx" setup>
import { toRaw } from "vue";

import type { TableColumnOption } from "@/components/table-widget/table-widget.vue";
import TableWidget from "@/components/table-widget/table-widget.vue";
import {
	getEnumOptionsField,
	RISK_HAZARDS_CONTROL_DANGER_STATUS_OPTIONS,
	RISK_HAZARDS_CONTROL_HAS_WARING_OPTIONS,
} from "@/config/enum-options";

import { injectState } from "../state";

import { whManagerBsTbEnterpriseGetListUsingPost } from "@/genapi/wh";
import { computedLoading } from "@/hooks/computed-loading";

defineOptions({ name: "Sheet" });

const {
	selectedList,
	tableRef,
	detailDialogRef,
	page: { records, isLoading, execute },
} = injectState()!;

watch(records, () => tableRef.value?.table?.clearSelection());

const [companyList] = computedLoading(async () => {
	const { data } = await whManagerBsTbEnterpriseGetListUsingPost({ data: {} });
	return data?.map((item) => ({ label: item.name, id: item.id })) ?? [];
});

const getCompanyName = (enterpriseId) => {
	const res = companyList.value?.filter((item) => {
		return item.id === enterpriseId;
	});
	return res[0] ? res[0].label : "--";
};

const columns: TableColumnOption<(typeof records.value)[0]>[] = [
	{
		type: "selection",
		fixed: "left",
		noExport: true,
	},
	{
		label: "序号",
		type: "index",
		fixed: "left",
		width: 80,
	},
	{
		label: "企业名称",
		formatter: (row) => getCompanyName(row.enterpriseId) ?? "-",
	},
	{
		label: "责任人员",
		formatter: (row) => row.masterName ?? "-",
	},
	{
		label: "联系电话",
		formatter: (row) => row.masterPhone ?? "-",
	},
	{
		label: "风险描述",
		formatter: (row) => row.dangerDesc ?? "-",
	},
	{
		label: "管控措施",
		formatter: (row) => row.handleDesc ?? "-",
	},
	{
		label: "风险日常检查表",
		formatter: (row) => row.dailyCheckFileName ?? "-",
	},
	{
		label: "风险状态",
		formatter: (row) =>
			getEnumOptionsField(
				RISK_HAZARDS_CONTROL_DANGER_STATUS_OPTIONS,
				"label",
				row.dangerStatus,
			) ?? "-",
	},
	{
		label: "提醒状态",
		formatter: (row) =>
			getEnumOptionsField(
				RISK_HAZARDS_CONTROL_HAS_WARING_OPTIONS,
				"label",
				row.hasWaring,
			) ?? "-",
	},

	{
		label: "操作",
		export: false,
		fixed: "right",
		width: "200",
		noExport: true,
		formatter(row) {
			return (
				<>
					<el-button
						size="small"
						type="primary"
						text
						onClick={() => detailDialogRef.value?.trigger("read", row)}
					>
						详情
					</el-button>
					<el-button
						size="small"
						type="primary"
						text
						onClick={() => detailDialogRef.value?.trigger("write", row)}
					>
						预警提示
					</el-button>
				</>
			);
		},
	},
];
</script>

<template>
	<TableWidget
		ref="tableRef"
		v-loading="isLoading"
		:data="records"
		:columns="columns"
		h="100%!"
		@selection-change="(rows:any) => (selectedList = toRaw(rows))"
	/>
</template>
